package org.gc.common.vo.security;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.gc.common.entity.security.AdminUserDetail;
import org.gc.common.vo.base.BaseVO;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;


/**
 * It implements Acegi Security's UserDetails interface.
 *
 */
public class UserVO extends BaseVO implements Serializable, UserDetails {
    private static final long serialVersionUID = 3832626162173359411L;
    
    private Long id;
    
    private String username;                    // required
    
    private String password;                    // required
    
    private String confirmPassword;
    
    private String passwordResetKey;            //密码重置的key
    
    private String name;                    // required
    
    private String email;                       // required; unique
    
    private String phoneNumber;
    
    private String mobile; //手机号码
    
    private Set<RoleVO> roles = new HashSet<RoleVO>();
    
    private Set<RoleGroupVO> roleGroups = new HashSet<RoleGroupVO>();
    
    private AdminUserDetailVO adminUserDetail;
    
    private boolean enabled;
   
    private boolean accountExpired;
    
    private boolean accountLocked;
    
    private boolean credentialsExpired;
    
    private Boolean adminUser;

	/**
     * Default constructor - creates a new instance with no values set.
     */
    public UserVO() {}

    /**
     * Create a new instance and set the username.
     * @param username login name for user.
     */
    public UserVO(final String username) {
        this.username = username;
    }

    public Long getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getPasswordResetKey() {
        return passwordResetKey;
    }

    public void setPasswordResetKey(String passwordResetKey) {
        this.passwordResetKey = passwordResetKey;
    }

    public String getEmail() {
        return email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public Set<RoleVO> getRoles() {
        return roles;
    }

    /**
     * Adds a role for the user
     * @param role the fully instantiated role
     */
    public void addRole(RoleVO role) {
        getRoles().add(role);
    }
    
    /**
     * @see org.springframework.security.userdetails.UserDetails#getAuthorities()
     * @return GrantedAuthority[] an array of roles.
     */
    public GrantedAuthority[] getAuthorities() {
        return roles.toArray(new GrantedAuthority[0]);
    }

    public boolean isEnabled() {
        return enabled;
    }
    
    public boolean isAccountExpired() {
        return accountExpired;
    }
    
    /**
     * @see org.springframework.security.userdetails.UserDetails#isAccountNonExpired()
     */
    public boolean isAccountNonExpired() {
        return !isAccountExpired();
    }

    public boolean isAccountLocked() {
        return accountLocked;
    }
    
    /**
     * @see org.springframework.security.userdetails.UserDetails#isAccountNonLocked()
     */
    public boolean isAccountNonLocked() {
        return !isAccountLocked();
    }

    public boolean isCredentialsExpired() {
        return credentialsExpired;
    }
    
    /**
     * @see org.springframework.security.userdetails.UserDetails#isCredentialsNonExpired()
     */
    public boolean isCredentialsNonExpired() {
        return !credentialsExpired;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setRoles(Set<RoleVO> roles) {
        this.roles = roles;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setAccountExpired(boolean accountExpired) {
        this.accountExpired = accountExpired;
    }
    
    public void setAccountLocked(boolean accountLocked) {
        this.accountLocked = accountLocked;
    }

    public void setCredentialsExpired(boolean credentialsExpired) {
        this.credentialsExpired = credentialsExpired;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof UserVO)) {
            return false;
        }

        final UserVO user = (UserVO) o;

        return !(username != null ? !username.equals(user.getUsername()) : user.getUsername() != null);

    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return (username != null ? username.hashCode() : 0);
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
                .append("username", this.username)
                .append("enabled", this.enabled)
                .append("accountExpired", this.accountExpired)
                .append("credentialsExpired", this.credentialsExpired)
                .append("accountLocked", this.accountLocked);

        GrantedAuthority[] auths = this.getAuthorities();
        if (auths != null) {
            sb.append("Granted Authorities: ");

            for (int i = 0; i < auths.length; i++) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(auths[i].toString());
            }
        } else {
            sb.append("No Granted Authorities");
        }
        return sb.toString();
    }

	public void setRoleGroups(Set<RoleGroupVO> roleGroups) {
		this.roleGroups = roleGroups;
	}

	public Set<RoleGroupVO> getRoleGroups() {
		return roleGroups;
	}

	public void setAdminUser(Boolean adminUser) {
		this.adminUser = adminUser;
	}

	public Boolean getAdminUser() {
		return adminUser;
	}

	public void setAdminUserDetail(AdminUserDetailVO adminUserDetail) {
		this.adminUserDetail = adminUserDetail;
	}

	public AdminUserDetailVO getAdminUserDetail() {
		return adminUserDetail;
	}
 
}
